package com.lobo.blog.controller.admin;

import com.github.pagehelper.PageInfo;
import com.lobo.blog.entity.Tag;
import com.lobo.blog.exception.AllReadyBeenDeletedException;
import com.lobo.blog.service.AdminTagService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @authoer:MacieSerenity
 * @createDate: 2022/9/29
 * @description:
 */
@Controller
@RequestMapping("/admin")
public class AdminTagsController {

    @Resource
    AdminTagService adminTagService;

    @RequestMapping(value = "/tags/{pageNum}", method = RequestMethod.GET)
    public String toAdminTagsPage(
            @PathVariable(value = "pageNum", required = false) Integer pageNum,
            Model model) {
        if (pageNum == null) {
            pageNum = 1;
        }
        PageInfo<Tag> tagPageInfo = adminTagService.getTags(pageNum);
        model.addAttribute("page", tagPageInfo);
        return "/admin/tags";
    }

    @RequestMapping(value = "/tags/input")
    public String addNewTag(@RequestParam("tagName") String tagName, Model model) {
        Tag tag = new Tag(0, tagName, new Date(), new Date(), 0, null);
        int result = adminTagService.saveTag(tag);
        if (result == 0) {
            model.addAttribute("msg", "添加失败！");
        } else {
            model.addAttribute("msg", "添加成功！");
        }
        PageInfo<Tag> tagPageInfo = adminTagService.getTags(1);
        model.addAttribute("page", tagPageInfo);
        return "/admin/tags";
    }

    @RequestMapping("/tags/delete/{id}")
    public String deleteType(@PathVariable("id") Integer id,
                             Model model) {
        try {
            if (adminTagService.deleteTagById(id) == 1) {
                model.addAttribute("msg", "删除成功！");
            } else {
                model.addAttribute("msg", "删除失败！该分类已经不存在！！");
            }
        } catch (AllReadyBeenDeletedException e) {
            throw new RuntimeException(e);
        }
        PageInfo<Tag> tagPageInfo = adminTagService.getTags(1);
        model.addAttribute("page", tagPageInfo);
        return "/admin/tags";
    }

    @RequestMapping(value = "/tags/update", method = RequestMethod.POST)
    public String updateTag(@RequestParam(value = "tagId") Integer tagId,
                            @RequestParam(value = "tagName") String tagName,
                            Model model) {
        Tag tag = new Tag(tagId, tagName, null, null, 0, null);
        int result = adminTagService.updateTag(tag);
        if (result != 0) {
            model.addAttribute("msg", "更新成功！");
        } else {
            model.addAttribute("msg", "更新失败！已有同名的分类！");
        }
        PageInfo<Tag> tagPageInfo = adminTagService.getTags(1);
        model.addAttribute("page", tagPageInfo);
        return "/admin/tags";
    }

}
